<template>
	<view>
		<view class="form">
			<view class="form_item">
				<view class="label">
					规格名称
				</view>
				<view class="input">
					<u--input placeholder="颜色型号不同写在名称里" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.text"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					出售价格
				</view>
				<view class="input">
					<u--input placeholder="请输入出售价格" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.price"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					库存数量
				</view>
				<view class="input">
					<u--input placeholder="请输入商品库存数量" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.num"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>

			<view class="form_title">
				<view class="title_name">
					添加展示图片
				</view>
			</view>
			<view class="pic_box">
				<view class="pic_item" v-if="form.image">
					<image class="pic_bg" @click="imgPreview()" :src="form.image" mode=""></image>
					<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);" @click="resup(index)">
						重新上传
					</view>
					<view class="delbox" @click="del(index)">
						<image class="del_icon" src="/static/img/business/picdel.png" mode=""></image>
					</view>
				</view>
				<view class="pic_item" v-else @click="up()">
					<image class="pic_bg" src="/static/img/my/picbg.png" mode=""></image>
					<view class="pic_item_name">
						点击上传
					</view>
				</view>
			</view>
			<view class="btn" @click="submit">
				确认{{id?'修改':'添加'}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		createSpec,
		getSpec,
		editSpec
	} from '@/api/product.js'
	import image from '../../uni_modules/uview-ui/libs/config/props/image';
	export default {
		data() {
			return {
				form: {},
				ggimgs: [],
				id: ''
			};
		},
		methods: {
			submit() {
				// this.form.image = this.ggimgs[0]
				if (!this.form.text) {
					uni.showToast({
						title: '请输入规格名称',
						icon: 'none'
					})
				}
				if (!this.form.price) {
					uni.showToast({
						title: '请输入出售价格',
						icon: 'none'
					})
				}
				if (!this.form.image) {
					uni.showToast({
						title: '请上传展示图片',
						icon: 'none'
					})
				}
				if (!this.form.num) {
					uni.showToast({
						title: '请输入库存数量',
						icon: 'none'
					})
				}
				if (this.id) {
					editSpec(this.form).then(res => {
						if (res.data.code == 1) {
							let ids = this.$store.state.spec
							// ids.push({...this.form,id:res.data.data.spec_id})
							ids.map((t, i) => {
								if (t.id == this.id) {
									ids[i] = this.form
								}
							})
							// console.log(ids)
							this.$store.commit('setspec', ids)
							let pages = getCurrentPages(); // 当前页面
							let beforePage = pages[pages.length - 2]; // 前一个页面
							// console.log(beforePage)
							uni.navigateBack({
								delta: 1,
								success: function() {
									beforePage.$vm.re(); // 执行前一个页面的刷新 （initData是我自己定义的方法）
								}
							});
							uni.showToast({
								title: '规格修改成功',
								icon: 'none'
							});
							// uni.navigateBack()
						}
					})
					return
				}
				createSpec(this.form).then(res => {
					if (res.data.code == 1) {
						let ids = this.$store.state.spec
						ids.push({
							...this.form,
							id: res.data.data.spec_id
						})
						this.$store.commit('setspec', ids)
						uni.showToast({
							title: '规格添加成功',
							icon: 'none'
						});
						uni.navigateBack()
					}
				})
			},
			async up(type) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						tempFilePaths.map(t => {
							uni.showLoading({
								title: '上传中'
							})
							uni.uploadFile({
								url: _this.$BASE_URL +
									'/api/common/upload', //仅为示例，非真实的接口地址
								filePath: t,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (uploadFileRes) => {
									_this.$set(_this.form, 'image', JSON.parse(
											uploadFileRes.data)
										.data
										.fullurl)
									// console.log(this.form)
									uni.hideLoading()
									uni.showToast({
										title: JSON.parse(uploadFileRes.data)
											.msg,
										icon: 'none'
									})
								},

							});
						})
					}
				});
			},
			imgPreview() {
				// console.log(urls)
				// uni.previewImage({
				// 	urls: this.form.image.join(',')
				// });
			},
			// 重新上传
			async resup(i, type) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: _this.$BASE_URL +
								'/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								uni.hideLoading()
								_this.$set(_this.form, 'image', JSON.parse(uploadFileRes.data)
									.data
									.fullurl)
								uni.showToast({
									title: JSON.parse(uploadFileRes.data).msg,
									icon: 'none'
								})
							},
						});
					}
				});
			},
			// 删除图片
			del() {
				// console.log(i, type)
				this.form.image = ''
			}
		},
		onLoad(option) {
			this.id = option.id
			if (this.id) {

				getSpec({
					id: this.id
				}).then(res => {
					// console.log(res)
					if (res.data.code == 1) {
						this.form = res.data.data
						// console.log(this.form)
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.form {
		padding: 10rpx 31rpx 70rpx;
		border-top: 2rpx solid #F4F6F8;

		.form_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #F4F6F8;
			height: 105rpx;

			.label {
				color: #333;
				font-size: 30rpx;
				font-weight: bold;
				padding-left: 10rpx;
				width: 210rpx;
			}

			.input {
				font-size: 30rpx;
				flex: 1;
			}

			.right_icon {
				width: 15rpx;
				height: 24rpx;
			}

			.right_icon_jia {
				width: 34rpx;
				height: 34rpx;
			}
		}

		.form_title {
			display: flex;
			padding: 45rpx 0 36rpx 10rpx;
			align-items: center;
			color: #333;
			font-size: 30rpx;
			font-weight: bold;

			.form_line {
				width: 8rpx;
				height: 28rpx;
				background: #FE712A;
				border-radius: 4rpx;
				margin-right: 10rpx;
			}
		}
	}

	.pic_box {
		display: flex;

		.pic_item {
			width: 198rpx;
			height: 198rpx;
			position: relative;
			margin-right: 34rpx;
		}

		.pic_bg {
			width: 198rpx;
			height: 198rpx;
			position: absolute;
			top: 0;
			left: 0;
		}

		.pic_item_name {
			text-align: center;
			width: 100%;
			position: absolute;
			bottom: 0;
			color: #fff;
			font-size: 26rpx;
			line-height: 60rpx;
		}
	}

	.btn {
		width: 687rpx;
		line-height: 114rpx;
		background: linear-gradient(153deg, #FF8717, #FF5F00);
		border-radius: 57rpx;
		text-align: center;
		font-size: 36rpx;
		color: #fff;
		margin-top: 77rpx;
	}

	.delbox {
		width: 29rpx;
		height: 25rpx;
		background: rgba(0, 0, 0, .5);
		border-radius: 7rpx 0 0 12rpx;

		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 0;
		right: 0;
	}

	.del_icon {
		width: 12rpx;
		height: 12rpx;
	}
</style>
